package Servlet;

import com.cyy.bean.product;
import com.cyy.dao.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/showIndex")
public class showIndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name=request.getSession().getAttribute("name").toString();
//       得到未下架的商品列表
        productDao productDao = new productDao();
        ArrayList<product> products = null;
        try {
            products = productDao.selectAll();
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
//        得到该用户的购物车商品数
        cartDao cartDao = new cartDao();
        int cartNum = 0;
        try {
            cartNum = cartDao.getMaxidByname(name);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
//        得到该用户的收藏商品数
        collectDao collectDao = new collectDao();
        int collectNum = 0;
        try {
            collectNum = collectDao.getMaxidByname(name);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
//        得到该用户的收到的未确认的邮件数
        int emailNum = 0;
        historyDao historyDao = new historyDao();
        try {
            emailNum = historyDao.getNumByname(name);
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
//        参数设置
        request.setAttribute("products", products);
        request.setAttribute("cartNum", cartNum);
        request.setAttribute("collectNum", collectNum);
        request.setAttribute("emailNum", emailNum);
        request.getRequestDispatcher("/customerMain.jsp").forward(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
